package com.kaifamiao.ioc.creation;

import org.springframework.beans.factory.FactoryBean;

/**
 * 1、实现 FactoryBean 接口并为其传入类型参数
 * 2、在 getObject 中完成指定类型对象的创建并返回该对象
 * 3、用 getObjectType 方法返回在 getObject 中所返回的对象的类型
 */
public class BusFactoryBean implements FactoryBean<Bus> {

    private String name ;

    public BusFactoryBean() {
        super();
        System.out.println( "[ BusFactoryBean ] - [ public BusFactoryBean() ]");
    }

    @Override
    public Bus getObject() throws Exception {
        System.out.println( "[ BusFactoryBean ] - [ public Bus getObject() throws Exception ]" );
        // 创建 Bus 实例
        Bus bus = new Bus();
        // 为 bus 设置 brand 属性值 (将当前实例的 name 字段值传递给brand)
        bus.setBrand( name );
        return bus;
    }

    @Override
    public Class<?> getObjectType() {
        return Bus.class; // 返回 getObject()方法的返回类型对应的Class实例
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println( "[ BusFactoryBean ] - [ public void setName(String) ]" );
        this.name = name;
    }

}
